package com.km.sglototo.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.km.sglototo.annotation.SysLog;
import com.km.sglototo.sys.entity.SysOrg;
import com.km.sglototo.sys.service.ISysOrgService;
import com.km.sglototo.utils.CommonResult;
import com.km.sglototo.utils.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author xt
 * @date 2022-01-11
 * 机构管理
 */
@Slf4j
@RestController
@RequestMapping("/sys/sysOrg")
public class SysOrgController {

    @Resource
    private  ISysOrgService ISysOrgService;

    @SysLog(MODULE = "sys", REMARK = "根据条件查询所有机构管理列表")
    @ApiOperation("根据条件查询所有机构管理列表")
    @GetMapping(value = "/list")
    public Object getSysOrgByPage(SysOrg entity,
                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "1000") Integer pageSize
    ) {
        try {
            return new CommonResult().success(ISysOrgService.page(new Page<SysOrg>(pageNum, pageSize), new QueryWrapper<>(entity)));
        } catch (Exception e) {
            log.error("根据条件查询所有机构管理列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "树形结构机构信息")
    @ApiOperation("树形结构机构信息")
    @GetMapping(value = "/treeList")
    public Object gettreeList() {
        try {
            return new CommonResult().success(ISysOrgService.treeList());
        } catch (Exception e) {
            log.error("根据条件查询所有机构管理列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "保存机构管理")
    @ApiOperation("保存机构管理")
    @PostMapping(value = "/create")
    public Object saveSysOrg(@RequestBody SysOrg entity) {
        try {
            entity.setOptime(new Date());
            if (ISysOrgService.save(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("保存机构管理：%s", e.getMessage(), e);
            return new CommonResult().failed(e.getMessage());
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "更新机构管理")
    @ApiOperation("更新机构管理")
    @PostMapping(value = "/update/{id}")
    public Object updateSysOrg(@RequestBody SysOrg entity) {
        try {
            if (ISysOrgService.updateById(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("更新机构管理：%s", e.getMessage(), e);
            return new CommonResult().failed(e.getMessage());
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "删除机构管理")
    @ApiOperation("删除机构管理")
    @GetMapping(value = "/delete/{id}")
    public Object deleteSysOrg(@ApiParam("机构管理id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("机构管理id");
            }
            if (ISysOrgService.removeById(id)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("删除机构管理：%s", e.getMessage(), e);
            return new CommonResult().failed(e.getMessage());
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "给机构管理分配机构管理")
    @ApiOperation("查询机构管理明细")
    @GetMapping(value = "/{id}")
    public Object getSysOrgById(@ApiParam("机构管理id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("机构管理id");
            }
            SysOrg coupon = ISysOrgService.getById(id);
            return new CommonResult().success(coupon);
        } catch (Exception e) {
            log.error("查询机构管理明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }

    }

    @ApiOperation(value = "批量删除机构管理")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.GET)
    @SysLog(MODULE = "sys", REMARK = "批量删除机构管理")
    public Object deleteBatch(@RequestParam("ids") List
            <Long> ids) {
        boolean count = ISysOrgService.removeByIds(ids);
        if (count) {
            return new CommonResult().success(count);
        } else {
            return new CommonResult().failed();
        }
    }



}


